package edu.biwu.session;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * 创建Session
 *  HttpServletRequest中的方法：
 *      public HttpSession getSession():
 *          如果当前服务器没有Session则创建一个session
 *          如果当前服务器有session则获取这个session
 *  HttpSession中的方法:
 *      public void setAttribute(String name, Object value): 向session添加name=value类似map结构数据
 */
@WebServlet("/session/generate")
public class GenerateSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.如果当前服务端没有session,会自动创建一个session
        HttpSession session = request.getSession();
        //2.向session中添加数据
        session.setAttribute("id","567");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
